<%@ page import="java.io.File" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="javax.imageio.ImageIO" %>
<%@ page import="java.awt.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <title>保存剪切后图片位置</title>
  </head>
  <body>

    <%
      out.print("x="+request.getParameter("x")+"<br>");
      out.print("y="+request.getParameter("y")+"<br>");
      out.print("weidth="+request.getParameter("w")+"<br>");
      out.print("height="+request.getParameter("h")+"<br>");

      //实现裁剪图片
      String img = "img/p.jpg";//原图位置
      try{
        BufferedImage i = ImageIO.read(new File(img));
        int w = Integer.parseInt(request.getParameter("w"));
        int h = Integer.parseInt(request.getParameter("h"));
        BufferedImage newimg = new BufferedImage(w,h,1);
        Graphics2D g = (Graphics2D)newimg.getGraphics();
        int x= Integer.parseInt(request.getParameter("x"));
        int y = Integer.parseInt(request.getParameter("y"));
        g.drawImage(i,0,0,w,h,x,y,x+w,y+h,null);
        ImageIO.write(newimg,"jpeg",new File("c:/p2.jpg"));//裁剪后图片位置
        g.dispose();
      }catch(Exception e){
          e.printStackTrace();
      }
    %>

  </body>
</html>
